// pages/record/index.js
var util=require('../../utils/utils.js');
Page({
  data: {
    page:1,
    pages:1,
    list:'',
    oneButton: [{text: '成为会员'}],
    showOneButtonDialog: false,
    id:'',
    date:'',
    end_time:'',
  },
  onLoad: function (options) {
    var DATE = util.formatTime(new Date());
    console.log(DATE)
    this.setData({
      end_time:DATE,
      date: DATE,
      id:options.user_id
    })
    this.getData(this.data.page)
  },
  // tapDialogButton(e) {
  //   this.setData({
  //     dialogShow: false,
  //     showOneButtonDialog: false
  //   })
  // },
  // tapOneDialogButton(e) {
  //   this.setData({
  //     showOneButtonDialog: true
  //   })
  // },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      // end_time: e.detail.value,
      start: e.detail.value
    })
  },
   bindDateChanges: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      end_time: e.detail.value
    })
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/archivesLists',
      data: {
        user_id:this.data.id,
        wxapp_id:10001,
        token:wx.getStorageSync("token"),
        page:this.data.pages,
        start_time:this.data.date,
        end_time:this.data.end_time
      },
      success: res => {
        console.log(res.data.data)
        if (pages > 1) {
          if (res.data.data != '') {
            this.setData({
              list: this.data.list.concat(res.data.data.data),
              pages: pages + 1
            })
          }
        } else {
          this.setData({
            list:res.data.data.data,
            pages: pages + 1
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  getData(page) {
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/archivesLists',
      data: {
        user_id:this.data.id,
        wxapp_id:10001,
        token:wx.getStorageSync("token"),
        page:this.data.page
      },
      success: res => {
        console.log(res.data.data)
        if (page > 1) {
          if (res.data.data != '') {
            this.setData({
              list: this.data.list.concat(res.data.data.data),
              page: page + 1
            })
          }
        } else {
          this.setData({
            list:res.data.data.data,
            page: page + 1
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
/**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData(this.data.page)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
   // 获取滚动条当前位置
   onPageScroll: function (e) {
    console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  gorRecord(){
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/isAddRecieves',
      method:'GET',
      data: {
        wxapp_id:10001,
        token:wx.getStorageSync("token")
      },
      success: res => {
        console.log(res.data.msg)
        if(res.data.msg=='success'){
            wx.navigateTo({ url: './record' });
        }
        else if(res.data.msg=='暂未开通会员'){
          wx.showModal({
            title: '成为会员',
            title: '请先成为会员后才能进行填写',
            confirmText:'成为会员',
            content: '',
            showCancel:false,
            success (res) {
              if (res.confirm) {
                wx.navigateTo({ url: '../member/member' });
                console.log('用户点击确定')
              }
            }
          })
        }
        else if(res.data.msg=='您已填写过一次档案'){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
    // wx.navigateTo({ url: './record' });
  },
  //跳转详情页
  detail(e){
    var dataset = e.currentTarget.dataset;
    var id = dataset.id;
    wx.navigateTo({
      url:'../records/index?id='+id
    })
  }
})